package Api;

import Util.HttpUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static Util.Tools.getMD5;

/**
 退款对账接口接口
 返回示例
 <?xml version="1.0" encoding="utf-8"?><root><ret_code>0000</ret_code><ret_msg></ret_msg><agent_id>1664502</agent_id><detail_data>20240109161249,H2401093558089AW,,4437105,0.01,0.01,0,微信支付,2024-01-09 16:28:54,2024-01-09 16:29:24,0.0002,0.0000|1704944332356019771000,H2401115037170AJ,,4448859,3.00,3.00,0,新快捷,2024-01-11 16:00:51,2024-01-11 16:03:56,0.0000,0.0000|83056456,H2401158652694AV,,4474463,0.01,0.01,0,微信支付,2024-01-15 17:33:24,2024-01-15 17:34:52,0.0002,0.0000|635910310,H2401169067250AT,,4478589,1.00,1.00,0,微信支付,2024-01-16 09:18:29,2024-01-16 09:19:16,0.0200,0.0000|nnk1747801913929281536,H2401180788562A7,,4491399,43.00,43.00,0,微信支付,2024-01-18 10:28:58,2024-01-18 10:30:07,0.8600,0.0000|nnk1747818402350632960,H2401180857489AT,,4491733,43.00,43.00,0,微信支付,2024-01-18 11:34:32,2024-01-18 11:35:26,0.8600,0.0000|nnk1747827130198138880,H2401180882721AV,,4491913,30.00,30.00,0,微信支付,2024-01-18 12:09:08,2024-01-18 12:09:37,0.6000,0.0000|nnk1747863359316873216,H2401180612845A2,,4492834,43.00,43.00,0,微信支付,2024-01-18 14:33:05,2024-01-18 14:34:21,0.8600,0.0000|nnk1747876045974949888,H2401181049171A6,,4493253,40.90,40.90,0,微信支付,2024-01-18 15:23:35,2024-01-18 15:25:34,0.8180,0.0000|994360938,H2401169067348AT,ceshitkuan,4493888,1.00,0.01,0,微信支付,2024-01-18 17:45:24,2024-01-18 17:45:44,0.0002,0.0000|</detail_data><total_page>1</total_page><total_count>10</total_count><sign>92732be5d483bf90268e9d5ee82a1385</sign></root>
 */
public class PaymentRefundCheck {

    public static void main(String[] args) {
        try {


            // 目标URL
            String url = "https://www.heepay.com/API/Check/PaymentRefundCheck.aspx";
            //需要的MD5密钥，商户后台退款版本一的密钥
            String key = "1C3A345D4F1E48B7A02B90AC";
            //商户编号，（汇付宝商户内码：七位整数数字）
            String agent_id = "1664502";
            //固定版本号
            String version = "1";
            String begin_time = "20240101";
            String end_time = "20240219";
            String page_size = "500";
            String page_index = "1";


            //拼接待加密参数
            StringBuilder builder = new StringBuilder();
            builder.append("agent_id=");
            builder.append(agent_id);
            builder.append("&begin_time=");
            builder.append(begin_time);
            builder.append("&end_time=");
            builder.append(end_time);
            builder.append("&key=");
            builder.append(key);
            builder.append("&page_index=");
            builder.append(page_index);
            builder.append("&page_size=");
            builder.append(page_size);
            builder.append("&version=");
            builder.append(version);

            String DateStr = builder.toString();
            String Date = DateStr.toLowerCase();
            System.out.println("待签名串：" + Date);

            //MD5加密得出签名结果
            String sign = getMD5(Date);


            // 请求参数，以Map形式表示
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("agent_id", agent_id);
            parameters.put("begin_time", begin_time);
            parameters.put("end_time", end_time);
            parameters.put("page_index", page_index);
            parameters.put("page_size", page_size);
            parameters.put("version", version);
            parameters.put("sign", sign);

            String response = HttpUtil.sendPost1(url, parameters);
            System.out.println(response);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
